﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MetalSoda
{
    class RouteCollection : IEnumerable<Route>
    {
        readonly ServiceHost FarHost;
        readonly ServiceHost Host;
        readonly Service FarService;
        readonly object[] Policies;

        internal RouteCollection(ServiceHost localHost, ServiceHost farHost, Service farService, object[] policies)
        {
            if (localHost == null) { throw new ArgumentNullException("localHost"); }
            if (farHost == null) { throw new ArgumentNullException("farHost"); }
            if (farService == null) { throw new ArgumentNullException("farService"); }
            Policies = policies ?? new object[0];
            this.Host = localHost;
            this.FarHost = farHost;
            this.FarService = farService;
        }

        public IEnumerator<Route> GetEnumerator()
        {
            foreach (var localDb in Host.Databases)
            {
                foreach (var farDb in FarHost.Databases)
                {
                    var route = new Route(localDb, farDb, FarService, Policies);
                    yield return route;
                }
            }            
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            IEnumerator<Route> enumerator = this.GetEnumerator();
            while (enumerator.MoveNext()) { yield return enumerator.Current; }
        }

    }
}
